

#include "unity.h"
#include "unity_fixture.h"

#include "Debug.h"
#include "BSP_Sleep.h"

#include <Reader/Reader.h>
#include <Reader/ReaderDesfire.h>
#include <Reader/desfire/mifare.h>
#include <Reader/desfire/mifare_key.h>



TEST_GROUP(Desfire);


TEST_SETUP(Desfire)
{
	reader.hfFlags = TAG_MASK_MIFARE;
	Reader_NFC_LoadProfile(reader.hfFlags, 0, 0, 0);
}

TEST_TEAR_DOWN(Desfire)
{
	Reader_NFC_RfOff();
}


TEST(Desfire, GetVersion)
{
	int ret;
	int i;
	SimpleTag_T simpleTag;
	for(i = 0; i < 100; i++){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(500);
		ret = Reader_NFC_Discovery(&simpleTag);
		if(ret){
			continue;
		}
		mifare_desfire_version_info version_info;
		//ret = MifareGetVersion(&reader.desfire, &version_info);
		ret = Reader_Desfire_GetVersion(&version_info);
		if(ret){
			LOG_E("MifareGetVersion error (%d)", ret);
			continue;
		}
		LOG_D("\nVersion info  PICC:");
		LOG_B("\n--> Hardware: ", &version_info.hardware, 7, 8);
		LOG_B("\n--> Software: ", &version_info.software, 7, 8);
		LOG_B("\n--> UID: ", version_info.uid, 7, 8);
		LOG_B("\n-->batch_number: ", version_info.batch_number,7, 8);
		LOG_B("\nwk yr: ", &version_info.production_week, 2, 8);
		break;
	}
}

IGNORE_TEST(Desfire, ReadStdFile)
{
	int i;
	int ret;
	SimpleTag_T simpleTag;
	for(i = 0; i < 100; i++){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(500);
		ret = Reader_NFC_Discovery(&simpleTag);
		if(ret){
			continue;
		}
		
		mifare_desfire_version_info version_info;
		ret = MifareGetVersion(&reader.desfire, &version_info);
		if(ret){
			LOG_E("MifareGetVersion error (%d)", ret);
			continue;
		}

		//select aid
		uint32_t aid = 1;
		ret = MifareSelectApplication(&reader.desfire, aid);
		if(ret){
			TEST_PRINTF("MifareSelectApplication error(%d)", ret);
			continue;
		}
		// auth
		uint8_t desfireKey[16] = {0};
		uint8_t desfireKeyId = 0;
		mifare_desfire_key key;
		MifareDesKeyNew(&key, desfireKey);
		ret = MifareAuthenticate(&reader.desfire, desfireKeyId, &key);
		if(ret){
			TEST_PRINTF("MifareAuthenticate error(%d)", ret);
			continue;
		}
		uint8_t desfireFID = 0;
		ssize_t data_size;
		uint8_t data[32];
		ret = MifareReadData(&reader.desfire, desfireFID, 0, 
						   8, data, sizeof(data), 
						   &data_size);
		if(ret){
			TEST_PRINTF("MifareReadData error(%d)", ret);
			continue;
		}
		LOG_B("\ndata: ", data,data_size, 8);
		break;
	}

}


IGNORE_TEST(Desfire, ChangeKey)
{
	int i;
	int ret;
	SimpleTag_T simpleTag;
	for(i = 0; i < 100; i++){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(500);
		ret = Reader_NFC_Discovery(&simpleTag);
		if(ret){
			continue;
		}
		
		mifare_desfire_version_info version_info;
		ret = Reader_Desfire_GetVersion(&version_info);
		if(ret){
			LOG_E("MifareGetVersion error (%d)", ret);
			continue;
		}

		//select aid
		uint32_t aid = 1;
		ret = Reader_Desfire_SelectApplication(aid);
		if(ret){
			continue;
		}
		// auth
		uint8_t keyData[16] = {0};
		uint8_t keyNo = 0;
		mifare_desfire_key key;
		Reader_Desfire_NewKey(&key, 0, keyData, sizeof(keyData));
		ret = Reader_Desfire_Authenticate(keyNo, &key, 0);
		if(ret){
			LOG_E("MifareAuthenticate error (%d)", ret);
			continue;
		}
		// Use an AES application master key
		mifare_desfire_key new_key;
		keyData[15] = 0x03;
		Reader_Desfire_NewKey(&new_key, 0, keyData, sizeof(keyData));

		ret = Reader_Desfire_ChangeKey(keyNo, &new_key, &key);
		if(ret){
			LOG_E("MifareChangeKey error (%d)", ret);
			continue;
		}
		
		/* Authenticate with the new master key */
		ret = Reader_Desfire_Authenticate(keyNo, &new_key, 0);
		if(ret){
			LOG_E("new key MifareAuthenticate error (%d)", ret);
			continue;
		}
		
		memset(keyData, 0, sizeof(keyData));
		keyData[15] = 0x03;
		Reader_Desfire_NewKey(&key, 0, keyData, sizeof(keyData));
		keyData[15] = 0x00;
		Reader_Desfire_NewKey(&new_key, 0, keyData, sizeof(keyData));
		ret = Reader_Desfire_ChangeKey(keyNo, &new_key, &key);
		if(ret){
			LOG_E("MifareChangeKey error (%d)", ret);
			continue;
		}
		
		ret = Reader_Desfire_Authenticate(keyNo, &new_key, 0);
		if(ret){
			LOG_E("new key MifareAuthenticate error (%d)", ret);
			continue;
		}
		LOG_D("\n OK!");
		break;
	}

}

TEST(Desfire, ReadRecord)
{
	int i;
	int ret;
	SimpleTag_T simpleTag;
	for(i = 0; i < 100; i++){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(500);
		ret = Reader_NFC_Discovery(&simpleTag);
		if(ret){
			continue;
		}
		
		mifare_desfire_version_info version_info;
		ret = Reader_Desfire_GetVersion(&version_info);
		if(ret){
			TEST_PRINTF("MifareGetVersion error (%d)", ret);
			continue;
		}

		//select aid
		uint32_t aid = 1;
		ret = Reader_Desfire_SelectApplication(aid);
		if(ret){
			TEST_PRINTF("Reader_Desfire_SelectApplication error (%d)", ret);
			continue;
		}
		// auth
		uint8_t keyData[16] = {0};
		uint8_t keyNo = 0;
		mifare_desfire_key key;
		Reader_Desfire_NewKey(&key, 0, keyData, sizeof(keyData));
		ret = Reader_Desfire_Authenticate(keyNo, &key, 0);
		if(ret){
			TEST_PRINTF("Reader_Desfire_Authenticate error (%d)", ret);
			continue;
		}
		uint8_t data [32];
		uint8_t file_no = 2;
		int data_size = 0;

		ret = Reader_Desfire_ReadRecords(file_no, 0, 0, data, sizeof(data), &data_size);
		if(ret){
			TEST_PRINTF("Reader_Desfire_ReadRecords error (%d)", ret);
			continue;
		}
		int j;
		TEST_MESSAGE("data\r\n");
		for(j = 0; j < data_size; j++){
			TEST_PRINTF("%X ", data[i]);
		}
		TEST_MESSAGE("\r\n");
		break;
	}
}

TEST(Desfire, GetEelementContent)
{

}



